package com.putong35.common.httprequest.volleyadaptee;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class QueueHelper {

    private RequestQueue mQueue;

    private static QueueHelper mInstance = null;


    public static QueueHelper getInstance() {
        if (mInstance == null) {
            mInstance = new QueueHelper();
        }
        return mInstance;
    }


    /**
     * 获得Volley网络请求队列实例
     *
     * @return
     */
    public RequestQueue getRequestQueue(Context context) {
        if (mQueue == null) {
            mQueue = Volley.newRequestQueue(context, null);
            mQueue.start();
        }
        return mQueue;
    }


    /**
     * 添加网络请求到请求队列中
     *
     * @param request 请求Request
     */
    public void addQueue(Context context, Request request) {
        getRequestQueue(context).add(request);
    }


    public void addQueue(Context context, Request request, Object tag) {
        request.setTag(tag);
        getRequestQueue(context).add(request);
    }


    public void cancelAll(Context context, Object tag) {
        getRequestQueue(context).cancelAll(tag);
    }

}
